package com.umeng.comm.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.qianxx.draw.R;
import com.qianxx.base.BaseAty;

import java.util.Calendar;

/**
 * Created by summersoft on 2016/5/5.
 */
public class DataPickerActivity extends BaseAty {

    // 设置创作时间的resultCode
    public static final int Result_Select = 1;

    public static final String YEAR = "year";
    public static final String MONTH = "month";
    public static final String DAY = "day";

    private View parent;

    private TextView btnok;

    private DatePicker picker;

    private int year;

    private int month;

    private int day;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parent = LayoutInflater.from(this).inflate(R.layout.datapicker,
                null);
        setContentView(parent);
        picker = (DatePicker) findViewById(R.id.datepicker);
        btnok = (TextView) findViewById(R.id.comfirm_btn);
        btnok.setOnClickListener(this);
        initTimePicker(parent);
    }

    private void initTimePicker(View view) {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        picker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                DataPickerActivity.this.year = year;
                DataPickerActivity.this.month = monthOfYear;
                DataPickerActivity.this.day = dayOfMonth;
            }
        });
        picker.setMaxDate(System.currentTimeMillis());
    }

    private void btnOK() {
        Intent data = new Intent();
//        ToastUtil.getInstance().toast(year + String.valueOf(month + 1) + day + " ");
        data.putExtra(YEAR, year);
        data.putExtra(MONTH, month + 1);
        data.putExtra(DAY, day);
        setResult(Result_Select, data);
        finish();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            // 点击确定创作时间
            case R.id.comfirm_btn:
                btnOK();
                break;
            default:
                break;
        }
    }

    public static void actionStart(Activity context, int requestCode) {
        Intent intent = new Intent(context, DataPickerActivity.class);
        context.startActivityForResult(intent, requestCode);
        context.overridePendingTransition(0, 0);
    }

}